package com.zsh.web.utils;/**
 * @Author: 003089
 * @Description:
 * @Date: 2019/6/3 11:36
 */

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.ArrayUtils;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
 import org.apache.commons.io.IOUtils;

/**
 * @Author: 003089
 * @Description:
 * @Date: 2019/6/3 11:36
 */
public class RSAUtil {


    private static final String CHARSET = "UTF-8";
    private static final String ALGORITHM_RSA = "RSA";
    private static final String ALGORITHM_RSA_SIGN = "SHA1withRSA";
    private static final int ALGORITHM_RSA_PRIVATE_KEY_LENGTH = 2048;
    /*加解密算法/工作模式/填充方式,Java6.0支持PKCS5Padding填充方式,BouncyCastle支持PKCS7Padding填充方式
    工作模式有ECB--电子密码本模式,CBC--加密分组链接模式,CFB--加密反馈模式,OFB--输出反馈模式,CTR--计数器模式
    其中ECB过于简单而不安全,已被弃用,相对的CBC模式是最安全的,http://www.moye.me/2015/06/14/cryptography_rsa/
    private static final String ALGORITHM_CIPHER_AES = "AES/ECB/PKCS5Padding";
    private static final String ALGORITHM_CIPHER_AES_PKCS7 = "AES/CBC/PKCS7Padding";
    private static final String ALGORITHM_CIPHER_DESede = "DESede/ECB/PKCS5Padding";*/
    private static final String ALGORITHM_CIPHER_ECB = "RSA/ECB/PKCS1Padding";

    private RSAUtil() {
    }

    /**
     * 初始化RSA算法密钥对
     *
     * @param keysize RSA1024已经不安全了,建议2048
     * @return 经过Base64编码后的公私钥Map, 键名分别为publicKey和privateKey
     */
    public static Map<String, String> initRSAKey(int keysize) {
        KeyPairGenerator kpg;
        try {
            kpg = KeyPairGenerator.getInstance(ALGORITHM_RSA);
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalArgumentException("No such algorithm-->[" + ALGORITHM_RSA + "]");
        }
        // 初始化KeyPairGenerator对象,不要被initialize()源码表面上欺骗,其实这里声明的size是生效的
        kpg.initialize(ALGORITHM_RSA_PRIVATE_KEY_LENGTH);
        // 生成密匙对
        KeyPair keyPair = kpg.generateKeyPair();
        // 得到公钥
        Key publicKey = keyPair.getPublic();
        String publicKeyStr = Base64.encodeBase64URLSafeString(publicKey.getEncoded());
        // 得到私钥
        Key privateKey = keyPair.getPrivate();
        String privateKeyStr = Base64.encodeBase64URLSafeString(privateKey.getEncoded());
        Map<String, String> keyPairMap = new HashMap<String, String>();
        keyPairMap.put("publicKey", publicKeyStr);
        keyPairMap.put("privateKey", privateKeyStr);

        return keyPairMap;
    }

    /**
     * RSA算法分段加解密数据
     *
     * @param cipher 初始化了加解密工作模式后的javax.crypto.Cipher对象
     * @param opmode 加解密模式,值为javax.crypto.Cipher.ENCRYPT_MODE/DECRYPT_MODE
     * @param datas  待分段加解密的数据的字节数组
     * @return 加密或解密后得到的数据的字节数组
     */
    private static byte[] rsaSplitCodec(Cipher cipher, int opmode, byte[] datas) throws Exception {
        int maxBlock = 0;
        if (opmode == Cipher.DECRYPT_MODE) {
            maxBlock = ALGORITHM_RSA_PRIVATE_KEY_LENGTH / 16;
        } else {
            maxBlock = ALGORITHM_RSA_PRIVATE_KEY_LENGTH / 8 - 11;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int offSet = 0;
        byte[] buff;
        int i = 0;
        try {
            while (datas.length > offSet) {
                if (datas.length - offSet > maxBlock) {
                    buff = cipher.doFinal(datas, offSet, maxBlock);
                } else {
                    buff = cipher.doFinal(datas, offSet, datas.length - offSet);
                }
                out.write(buff, 0, buff.length);
                i++;
                offSet = i * maxBlock;
            }
        } catch (Exception e) {

            throw new Exception("加解密阀值为[" + maxBlock + "]的数据时发生异常", e);
        }
        byte[] resultDatas = out.toByteArray();
        IOUtils.closeQuietly(out);
        return resultDatas;
    }

    /**
     * @param data 待加密的明文字符串
     * @param key  RSA私钥字符串
     * @return RSA私钥加密后的经过Base64编码的密文字符串
     */
    public static String buildRSAEncryptByPrivateKey(String data, String key) throws Exception {
        try {
            // 通过PKCS#8编码的Key指令获得私钥对象
            PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(key));
            KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_RSA);
            Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
            // encrypt
            Cipher cipher = Cipher.getInstance(ALGORITHM_CIPHER_ECB);
            cipher.init(Cipher.ENCRYPT_MODE, privateKey);

            byte[] dataReturn = new byte[]{};
            // StringBuilder sb = new StringBuilder();
            for (int i = 0; i < data.getBytes(CHARSET).length; i += 100) {
                byte[] doFinal = cipher.doFinal(ArrayUtils.subarray(data.getBytes(CHARSET), i, i + 100));
                // sb.append(new String(doFinal));
                dataReturn = ArrayUtils.addAll(dataReturn, doFinal);
            }

            return Base64.encodeBase64URLSafeString(dataReturn);
        } catch (Exception e) {
            throw new Exception("加密字符串[" + data + "]时遇到异常", e);
        }
    }

    /**
     * RSA算法公钥加密数据
     *
     * @param data 待加密的明文字符串
     * @param key  RSA公钥字符串
     * @return RSA公钥加密后的经过Base64编码的密文字符串
     */
    public static String buildRSAEncryptByPublicKey(String data, String key) throws Exception {
        try {
            // 通过X509编码的Key指令获得公钥对象
            X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(Base64.decodeBase64(key));
            KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_RSA);
            Key publicKey = keyFactory.generatePublic(x509KeySpec);
            // encrypt
            Cipher cipher = Cipher.getInstance(ALGORITHM_CIPHER_ECB);
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);

            byte[] dataReturn = new byte[]{};
            // StringBuilder sb = new StringBuilder();
            for (int i = 0; i < data.getBytes(CHARSET).length; i += 100) {
                byte[] doFinal = cipher.doFinal(ArrayUtils.subarray(data.getBytes(CHARSET), i, i + 100));
                // sb.append(new String(doFinal));
                dataReturn = ArrayUtils.addAll(dataReturn, doFinal);
            }
            return Base64.encodeBase64URLSafeString(dataReturn);
        } catch (Exception e) {
            throw new Exception("加密字符串[" + data + "]时遇到异常", e);
        }
    }

    /**
     * RSA算法公钥解密数据
     *
     * @param data 待解密的经过Base64编码的密文字符串
     * @param key  RSA公钥字符串
     * @return RSA公钥解密后的明文字符串
     */
    public static String buildRSADecryptByPublicKey(String data, String key) throws Exception {
        try {
            // 通过X509编码的Key指令获得公钥对象
            X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(Base64.decodeBase64(key));
            KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_RSA, new BouncyCastleProvider());
            Key publicKey = keyFactory.generatePublic(x509KeySpec);
            // decrypt
            Cipher cipher = Cipher.getInstance(ALGORITHM_CIPHER_ECB);
            cipher.init(Cipher.DECRYPT_MODE, publicKey);
            return new String(rsaSplitCodec(cipher, Cipher.DECRYPT_MODE, Base64.decodeBase64(data)), CHARSET);
        } catch (Exception e) {
            throw new Exception("解密字符串[" + data + "]时遇到异常", e);
        }
    }

    /**
     * RSA算法私钥解密数据
     *
     * @param data 待解密的经过Base64编码的密文字符串
     * @param key  RSA私钥字符串
     * @return RSA私钥解密后的明文字符串
     */
    public static String buildRSADecryptByPrivateKey(String data, String key) throws Exception {
        try {
            // 通过PKCS#8编码的Key指令获得私钥对象
            PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(key));
            KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_RSA);
            Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
            // decrypt
            Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            return new String(rsaSplitCodec(cipher, Cipher.DECRYPT_MODE, Base64.decodeBase64(data)), CHARSET);
        } catch (Exception e) {
            throw new Exception("解密字符串[" + data + "]时遇到异常", e);
        }
    }

    /**
     * RSA算法使用私钥对数据生成数字签名
     *
     * @param data 待签名的明文字符串
     * @param key  RSA私钥字符串
     * @return RSA私钥签名后的经过Base64编码的字符串
     */
    public static String buildRSASignByPrivateKey(String data, String key) throws Exception {
        try {
            // 通过PKCS#8编码的Key指令获得私钥对象
            PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(key));
            KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_RSA);
            PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
            // sign
            Signature signature = Signature.getInstance(ALGORITHM_RSA_SIGN);
            signature.initSign(privateKey);
            signature.update(data.getBytes(CHARSET));
            return Base64.encodeBase64URLSafeString(signature.sign());
        } catch (Exception e) {
            throw new Exception("签名字符串[" + data + "]时遇到异常", e);
        }
    }

    /**
     * RSA算法使用公钥校验数字签名
     *
     * @param data 参与签名的明文字符串
     * @param key  RSA公钥字符串
     * @param sign RSA签名得到的经过Base64编码的字符串
     * @return true--验签通过,false--验签未通过
     */
    public static boolean buildRSAverifyByPublicKey(String data, String key, String sign) throws Exception {
        try {
            // 通过X509编码的Key指令获得公钥对象
            X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(Base64.decodeBase64(key));
            KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_RSA, new BouncyCastleProvider());
            PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);
            // verify
            Signature signature = Signature.getInstance(ALGORITHM_RSA_SIGN);
            signature.initVerify(publicKey);
            signature.update(data.getBytes(CHARSET));
            return signature.verify(Base64.decodeBase64(sign));
        } catch (Exception e) {
            throw new Exception("验签字符串[" + data + "]时遇到异常", e);
        }
    }



    public static String sign(String content, String privateKey) {
        try {
            PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey.getBytes()));
            KeyFactory keyf = KeyFactory.getInstance("RSA");
            PrivateKey priKey = keyf.generatePrivate(priPKCS8);
            Signature signature = Signature.getInstance("SHA256WithRSA");
            signature.initSign(priKey);
            signature.update(content.getBytes(CHARSET));
            return Base64.encodeBase64URLSafeString(signature.sign());
        } catch (Exception e) {
           e.printStackTrace();
        }
        return null;
    }

    public static boolean checkSign(String content, String sign, String publicKey){
        try {
            // 通过X509编码的Key指令获得公钥对象
            X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKey));
            KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_RSA, new BouncyCastleProvider());
            PublicKey key = keyFactory.generatePublic(x509KeySpec);
            // verify
            Signature signature = Signature.getInstance("SHA256WithRSA");
            signature.initVerify(key);
            signature.update(content.getBytes(CHARSET));
            return signature.verify(Base64.decodeBase64(sign));
        }catch (Exception e) {
          e.printStackTrace();
        }
        return false;
    }

    public static void main(String[] args) throws Exception{
        String publicKey="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCsrhK2FJnlE8JPtHy79mJL1YCY\n" +
                "IWbR6SW5wLLwZV4U2sMrl/1nb1UVAvPVCftYacnz7RU3XdPyVdWer21xunf32e06\n" +
                "fLrvt//VTLW9Sou7JlX4AL1m2cPZk0COswZWKTSgiF9MOI9Xg3dGpOo4TIjoCthr\n" +
                "6qKsRcVhipwNvaRq3QIDAQAB";
        String privateKey="MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKyuErYUmeUTwk+0\n" +
                "fLv2YkvVgJghZtHpJbnAsvBlXhTawyuX/WdvVRUC89UJ+1hpyfPtFTdd0/JV1Z6v\n" +
                "bXG6d/fZ7Tp8uu+3/9VMtb1Ki7smVfgAvWbZw9mTQI6zBlYpNKCIX0w4j1eDd0ak\n" +
                "6jhMiOgK2GvqoqxFxWGKnA29pGrdAgMBAAECgYEAgwaSrBZ1r5yOlGwfOYB6lZAr\n" +
                "Q0QVALIoLgpJvJ8uBCT6ctmhLhsMNCVJXx1z2mJrGuuKbbiEWh1HOmFiailTXT5L\n" +
                "TREuJXx0qsU+os5vVQeccRhDShj9YMOwnBG+Xp5KT2qLSG9phrzyGONjW19TYMO6\n" +
                "0rVYwVL6yoMsk7D9TwECQQDcu34klYjlNsyNGAqsLhpp3/phRRt0TjgZhFjFECJ8\n" +
                "7WEVFz5y7oIPKmmJadNmck/cFEC3CVoJn3EHgq7b3/IbAkEAyEUduLTFKaGgAfTN\n" +
                "PRP5FNWTQvKubAKtVL6u1XHVoDTkPm2uQJf9H1VRTPEUfmStzxPJGa0jOYhMxlt6\n" +
                "E24mZwJAU8RXl7yj6hDEydrQ2gzY+0sDsvxN8fJpOc3O/mIkJHLVf/KFQYqgEu9O\n" +
                "g9aEYoGJKwUIli3XXIWEmIwQneK79wJABa/YwTb45k69oXCXJ2Xay9movdzwUQbe\n" +
                "byqfAlaiL1NxK8inixQFzw74KQwrfc9UFC5GpqEBABA7xeIxHD4jfwJAH7FcOx0U\n" +
                "EnUdgqIt7XpeMk2+mTJm2P981dpwpYIXoXyd3Nq+cRs7FpEmQWyYy/x81eaywR5Y\n" +
                "XXylgsjRFHR2pg==";

        String tt = buildRSAEncryptByPrivateKey("傻逼",privateKey);
        System.out.println(buildRSADecryptByPublicKey(tt,publicKey));

        String sign=sign("傻逼",privateKey);
        System.out.println(sign);
        boolean flag= checkSign("傻逼",sign,publicKey);
        System.out.println(flag);

    }
}
